package com.javafactroy.kunlong.test.hutool;

import com.github.jsonzou.jmockdata.JMockData;
import com.github.jsonzou.jmockdata.MockConfig;
import com.google.common.base.Throwables;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.javafactroy.kunlong.DbAgent;
import com.javafactroy.kunlong.client.feign.dto.ApiResult;
import com.javafactroy.kunlong.data.entity.TSysUserApiEntity;
import com.javafactroy.kunlong.data.service.TSysUserService;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import com.javafactroy.kunlong.base.utils.GeneralThreadPool;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author raymond.lei@163.com
 * @date 2023-10-29 12:29:16
 */

@SpringBootTest(classes = {DbAgent.class}, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ExtendWith(SpringExtension.class)
@Slf4j
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestGuava {

    @Autowired
    TSysUserService sysUserService;

    @Test
    public void test001_bimap() {
        HashBiMap<String, String> userNameAndIdMap = HashBiMap.create();
        userNameAndIdMap.put("1", "a");
        userNameAndIdMap.put("2", "b");
        log.info(" get={}", userNameAndIdMap.get("1"));
        log.info("{} , {}", userNameAndIdMap, userNameAndIdMap.inverse().get("a"));
        //tps://blog.csdn.net/zch981964/article/details/129085740
        try {
            throw new RuntimeException("ddd");
        } catch (Exception e) {
            String str = Throwables.getStackTraceAsString(e);
            log.info("str={}", str);
        }
    }

    @Test
    public void test002_selectByPkey() {
        TSysUserApiEntity api = sysUserService.selectByPkey(1l);
        log.info("entity={}", CommonUtils.toJSONString(api));

    }


    public TSysUserApiEntity mock() {
        MockConfig mockConfig = new MockConfig()
                .doubleRange(1e-9, 1111222221119999.99999d)
                .floatRange(1e-8f, 111111122221119999.99999f)
                .decimalScale(52).excludes("id", "orderBys") // 设置小数位数为3，默认是2
                .subConfig("status").intRange(0, 9).globalConfig();
        TSysUserApiEntity dto = JMockData.mock(TSysUserApiEntity.class, mockConfig);
        log.info("dto={}", CommonUtils.toJSONString(dto));
        return dto;
    }

    @Test
    public void test003_save() {
        TSysUserApiEntity entity = mock();// new TSysUserApiEntity();
        entity.setUsername("11111");
        entity.setId(null);
        sysUserService.save(entity);
        log.info("entity = {}", entity);

    }

    static class CallableTask<T> implements Callable<T> {

        @Override
        public T call() throws Exception {
            return null;
        }
    }

    public static class GroupTask<T> {
        int threadNum;

        public ApiResult<T> run(List<T> tasks) {
            List<List<T>> taskList = Lists.partition(tasks, threadNum);
            List<Future<T>> futureList = new ArrayList<>();
            taskList.forEach(i -> {
                Future<T> submit = runThread(i);
                futureList.add(submit);
            });
            //futureList.forEach(i->i.get());
            return ApiResult.success();
        }

        Future<T> runThread(List<T> task) {
            Future<T> submit = GeneralThreadPool.inst().submit(new CallableTask<T>());

            return submit;
        }
    }

    @Test
    public void test004_guavaThread() throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        try {
            List<ListenableFuture<Integer>> futureList = new ArrayList<>();
            ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executorService);
            for (int i = 0; i < 5; i++) {
                int finalI = i;
                Future<?> submit = listeningExecutorService.submit(() -> {
                    TimeUnit.SECONDS.sleep(finalI);
                    return finalI;
                });
                futureList.add((ListenableFuture<Integer>) submit);
            }
            List<Integer> integers = Futures.allAsList(futureList).get();
            integers.forEach(System.out::println);
        } finally {
            executorService.shutdown();
        }
    }

}

